//
//  PPTabBar.m
//  PPWeibo
//
//  Created by Dreamsoul on 14-11-23.
//  Copyright (c) 2014年 杭州电子科技大学. All rights reserved.
//

#import "PPTabBar.h"
#import "PPTabBarButton.h"
@interface PPTabBar()
@property (nonatomic,weak)PPTabBarButton *selectedButton;
//@property (nonatomic,weak)UIButton *plusButton;
@property (nonatomic,strong)NSMutableArray *tabBarArray;
@end
@implementation PPTabBar
/*
- (UIButton *)plusButton{
    if (_plusButton==nil) {
        _plusButton = [UIButton buttonWithType:UIButtonTypeCustom];
    }
    return _plusButton;
}
 */
- (NSMutableArray *)tabBarArray{
    if (_tabBarArray==nil) {
        _tabBarArray = [NSMutableArray array];
    }
    return _tabBarArray;
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        if (!iOS7) {
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithName: @"tabbar_background"]];
        }
        /*
        [self.plusButton setBackgroundImage:[UIImage imageWithName:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [self.plusButton setBackgroundImage:[UIImage imageWithName:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        [self.plusButton setImage:[UIImage imageWithName:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [self.plusButton setImage:[UIImage imageWithName:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        self.plusButton.bounds = CGRectMake(0, 0, self.plusButton.currentBackgroundImage.size.width, self.plusButton.currentBackgroundImage.size.height);
        [self addSubview:self.plusButton];
         */
    }
    return self;
}


- (void)tabBarButtonWithItem:(UITabBarItem *)item{
    PPTabBarButton *button = [[PPTabBarButton alloc] init];
    [self addSubview:button];
    button.item=item;
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
    [self.tabBarArray addObject:button];
    if (self.tabBarArray.count == 1) {
        [self buttonClick:button];
    }
}
- (void)buttonClick:(PPTabBarButton *)button{
    if ([self.delegate respondsToSelector:@selector(tabBar:didSelectedButtonFrom:to:)]) {
        [self.delegate tabBar:self didSelectedButtonFrom:self.selectedButton.tag to:button.tag];
    }
    self.selectedButton.selected = NO;
    button.selected=YES;
    self.selectedButton = button;
}
- (void)layoutSubviews{
    
    //self.plusBu tton.center = CGPointMake(self.frame.size.width*0.5, self.frame.size.height*0.5);
    
    CGFloat buttonY = 0;
    CGFloat buttonH = self.frame.size.height;
    CGFloat buttonW = self.frame.size.width /self.subviews.count;
    for(int index = 0;index<self.tabBarArray.count;index++){
        PPTabBarButton *button = self.tabBarArray[index];
        CGFloat buttonX = buttonW*index;
        /*
        if (index>1) {
            buttonX = buttonX + buttonW;
        }
         */
        button.frame=CGRectMake(buttonX, buttonY, buttonW, buttonH);
        button.tag = index;
    }
}
@end
